import requests
import urllib
import datetime
import urllib2
from bs4 import BeautifulSoup
url = "https://unsplash.com/"
# add a request process to make it completely
request = urllib2.Request(url)
# add try-except module to return error infomation
try:
 r = urllib2.urlopen(request)
except urllib2.HTTPError,e:
 print e.code
except urllib.URLError,e:
 print e.reason
else: 
 print "OK"
# the list(a) is used to store download urls in html
a= []
# display a regular input
htmls = r.read()
soup = BeautifulSoup(htmls,'html.parser')
# find all links
for line in soup.find_all('a'):
 a.append(line.get('href'))
# filter all download links
result = []
for line in a:
 if 'download' in line:
  result.append(line)
new_result = []
# de-weight
for l in result:
 if l not in new_result:
  new_result.append(l)
print(len(new_result))
for num in range(0,len(new_result)):
 nowTime = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
 jpg_name = "jpg_"+str(nowTime)+".jpg"
 print(new_result[num])
 print("start *****************")
 urllib.urlretrieve(new_result[num],jpg_name)
 print(jpg_name+" download finished")
 print(" end  *****************")
